from neo_db.config import graph, CA_LIST, similar_words
from spider.show_profile import get_profile
import codecs
import os
import json
import base64

def query(name):
    data = graph.run(
    "match(p )-[r]->(n:Person{Name:'%s'}) return  p.Name,r.relation,n.Name,p.cate,n.cate\
        Union all\
    match(p:Person {Name:'%s'}) -[r]->(n) return p.Name, r.relation, n.Name, p.cate, n.cate" % (name,name)
    )
    data = list(data)
    return get_json_data(data)
def get_json_data(data):
    json_data={'data':[],"links":[]}
    d=[]
    
    
    for i in data:
        # print(i["p.Name"], i["r.relation"], i["n.Name"], i["p.cate"], i["n.cate"])
        d.append(i['p.Name']+"_"+i['p.cate'])
        d.append(i['n.Name']+"_"+i['n.cate'])
        d=list(set(d))
    name_dict={}
    count=0
    for j in d:
        j_array=j.split("_")
    
        data_item={}
        name_dict[j_array[0]]=count
        count+=1
        data_item['name']=j_array[0]
        data_item['category']=CA_LIST[j_array[1]]
        json_data['data'].append(data_item)
    for i in data:
   
        link_item = {}
        
        link_item['source'] = name_dict[i['p.Name']]
        
        link_item['target'] = name_dict[i['n.Name']]
        link_item['value'] = i['r.relation']
        json_data['links'].append(link_item)

    return json_data
# f = codecs.open('./static/test_data.json','w','utf-8')
# f.write(json.dumps(json_data,  ensure_ascii=False))
def get_KGQA_answer(array):
    # print('***************')
    # print(array)
    data_array=[]
    print(array)
    for i in range(len(array)-2):
        if i==0:
            name=array[0]
            # print(name)
        else:
            name=data_array[-1]['p.Name']
            # print('2')
        print(similar_words[array[i+1]], similar_words[array[i+1]], name)
        data = graph.run(
            "match(p)-[r:%s{relation: '%s'}]->(n:Person{Name:'%s'}) return  p.Name,n.Name,r.relation,p.cate,n.cate" % (
                similar_words[array[i+1]], similar_words[array[i+1]], name)
        )
        # print(type(data))
        data = list(data)
        print(data)
        data_array.extend(data)
        # print("==="*36)
        with open("./spider/images/"+"%s.jpg" % (str(data_array[-1]['p.Name'])), "rb") as image:
            base64_data = base64.b64encode(image.read())
            b=str(base64_data)
    # print(str(data_array[-1]['p.Name']))
    # print("===" * 36)
    return [get_json_data(data_array), get_profile(str(data_array[-1]['p.Name'])), b.split("'")[1]]
# get_KGQA_answer(['李云','同乡','的'])

def get_answer_profile(name):
    with open("./spider/images/"+"%s.jpg" % (str(name)), "rb") as image:
        base64_data = base64.b64encode(image.read())
        b = str(base64_data)
    return [get_profile(str(name)), b.split("'")[1]]



# def createData():
#     data = []
#     with  open('relation.txt','r',encoding='utf-8') as lines:
#         for line in lines:
#             line = line.strip()
#             # print(line)
#             name1 = line.split(',',4)[0]
#             name2 = line.split(',',4)[1]
#             relation = line.split(',',4)[2]
#             cate1 = line.split(',',4)[3]
#             cate2 = line.split(',',4)[4]
#             temp = {}
#             temp["p.Name"] = name1
#             temp["r.relation"] = relation
#             temp["n.Name"] = name2
#             temp["p.cate"] = cate1
#             temp["n.cate"] = cate2
#             data.append(temp)
#     json_data = get_json_data(data)
#     jd = json.dumps(json_data, ensure_ascii=False)
#     f = codecs.open('test_data.json', 'w', 'utf-8')
#     f.write(jd)
#
# createData();
